package phone.qyg.com.qyg.adapter;

import android.content.Context;
import android.view.View;

import com.app.adapter.recycler.MultiItemTypeAdapter;
import com.app.adapter.recycler.base.ItemViewDelegate;
import com.app.adapter.recycler.base.ViewHolder;
import com.app.application.QXApplication;
import com.app.config.ApplicationConfig;
import com.app.utils.CommonUtil;
import com.app.utils.OptionImageUtils;
import com.app.utils.SharePrefUtil;
import com.app.utils.TransformController;

import java.text.MessageFormat;
import java.util.List;

import phone.qyg.com.qyg.R;
import phone.qyg.com.qyg.activity.LoginActivity;
import phone.qyg.com.qyg.activity.ReleaseDiscussActivity;
import phone.qyg.com.qyg.model.Discuss;

/**
 * 作者：lblbh on 2017/12/6 19:29
 * 邮箱：lanbuhan@163.com
 * 功能：TODO
 */
public class DiscussAdapter extends MultiItemTypeAdapter<Object> {

    String REPLY_CONTENT = "<font color='#01aefd'>{0}</font> 回复 <font color='#01aefd'>{1}</font> {2}";

    int type;

    public DiscussAdapter(Context context, List<Object> datas,int type) {
        super(context, datas);
        this.type = type;
        addItemViewDelegate(new DiscussItem());
        addItemViewDelegate(new DiscussItemSecong());
        addItemViewDelegate(new EmptyItem());
    }

    /**
     * 一级回复
     */
    public class DiscussItem implements ItemViewDelegate<Object> {

        @Override
        public int getItemViewLayoutId() {
            return R.layout.item_discuss;
        }

        @Override
        public boolean isForViewType(Object item, int position) {
            return (item instanceof Discuss);
        }

        @Override
        public void convert(ViewHolder holder, Object obj, int position) {
            //讨论
            final Discuss model = (Discuss) obj;
            holder.setText(R.id.item_name,model.userName);
            holder.setText(R.id.item_address,model.dateDiff);
            holder.setText(R.id.item_summary,model.content);
            holder.setStar(R.id.rating_bar,model.star);
            holder.setVisible(R.id.rating_bar,(type == ApplicationConfig.ACT)?false:true);
            holder.setVisible(R.id.item_foot_divide,(model.isFoot)?true:false);
            holder.setImage(R.id.item_img,model.fixImgUrl, OptionImageUtils.getPersonIcon());

            holder.setOnClickListener(R.id.item_reply, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(!CommonUtil.isRequestStr(SharePrefUtil.getString(SharePrefUtil.KEY.APP_USER,""))){
                        TransformController.transformController(QXApplication.getContext(),LoginActivity.class);
                        return;
                    }
                    model.type = type;
                    TransformController.transformControllerModel(QXApplication.getContext(), ReleaseDiscussActivity.class,model);
                }
            });
        }
    }

    /**
     * 二级回复
     */
    public class DiscussItemSecong implements ItemViewDelegate<Object> {

        @Override
        public int getItemViewLayoutId() {
            return R.layout.item_discuss_second;
        }

        @Override
        public boolean isForViewType(Object item, int position) {
            return (item instanceof phone.qyg.com.qyg.model.DiscussItem );
        }

        @Override
        public void convert(ViewHolder holder, Object obj, int position) {
            //讨论
            final phone.qyg.com.qyg.model.DiscussItem  model = (phone.qyg.com.qyg.model.DiscussItem ) obj;
            //头部
            holder.setVisible(R.id.item_head_divide,(model.head == 0)?true:false);
            //尾部
            holder.setVisible(R.id.item_foot_divide,(model.isFoot)?true:false);

            holder.setTextHTMLTwo(R.id.item_name_content, MessageFormat.format(REPLY_CONTENT,
                    CommonUtil.setAttributeText(model.userName),
                    CommonUtil.setAttributeText(model.reply_user_name),
                    CommonUtil.setAttributeText(model.content)));

            holder.setText(R.id.item_address,model.dateDiff);

            holder.setOnClickListener(R.id.item_reply, new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(!CommonUtil.isRequestStr(SharePrefUtil.getString(SharePrefUtil.KEY.APP_USER,""))){
                        TransformController.transformController(QXApplication.getContext(),LoginActivity.class);
                        return;
                    }
                    model.type = type;
                    TransformController.transformControllerModel(QXApplication.getContext(), ReleaseDiscussActivity.class,model);
                }
            });
        }
    }

    public class EmptyItem implements ItemViewDelegate<Object> {

        @Override
        public int getItemViewLayoutId() {
            return R.layout.item_discuss_empty;
        }

        @Override
        public boolean isForViewType(Object item, int position) {
            return (item instanceof String);
        }

        @Override
        public void convert(ViewHolder holder, Object obj, int position) {
            //kong
        }
    }
}
